package com.cars.rocket.controller;

import com.cars.rocket.domain.DO.User;
import com.cars.rocket.domain.DTO.UserCharge;
import com.cars.rocket.rocketMQ.producer.RocketMQProducer;
import com.cars.rocket.rocketMQ.producer.RocketMQTXProducer;
import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: caoqianfan
 * @date: 2023/10/17 14:14
 */
@RestController
@RequestMapping("/rocketmq")
public class RocketMQController {

    @Resource
    private RocketMQProducer producer;

    @Resource
    private RocketMQTXProducer rocketMQTXProducer;

    @PostMapping("/send")
    public void send(@RequestBody User user) {
        producer.send(user);
    }

    @PostMapping("/sendTag")
    public ResponseEntity<SendResult> sendTag() {
        SendResult sendResult = producer.sendTagMsg("带有tag的字符消息");
        return ResponseEntity.ok(sendResult);
    }

    @PostMapping("/charge")
    public ResponseEntity<TransactionSendResult> charge(@RequestBody UserCharge userCharge) {
        TransactionSendResult sendResult = rocketMQTXProducer.sendHalfMsg(userCharge);
        return ResponseEntity.ok(sendResult);
    }


}